import { createRouter, createWebHistory } from "vue-router";
import Layout from "@/layout/Layout.vue";
import Login from "@/views/Login.vue";
import UserList from "@/views/user/UserList.vue";
import EmployeeChart from "@/views/EmployeeChart.vue";
import NotFound from "@/views/error/404.vue";
import RoleList from '@/views/role/RoleList.vue'
import PermissionList from '@/views/permission/PermissionList.vue'

const routes = [
  {
    path: "/login",
    name: "Login",
    component: Login,
  },
  {
    path: "/",
    component: Layout,
    redirect: "/users",
    children: [
      { path: "users", name: "UserList", component: UserList },
      { path: "chart", name: "Chart", component: EmployeeChart },
      { path: 'role', name: 'RoleList', component: RoleList },
      { path: 'permission', name: 'PermissionList', component: PermissionList },
    ],
  },
  {
    path: "/:pathMatch(.*)*",
    name: "NotFound",
    component: NotFound,
  },
];

export const router = createRouter({
  history: createWebHistory(),
  routes,
});

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem("token");
  if (to.path !== "/login" && !token) {
    next("/login");
  } else {
    next();
  }
});
